Stable Diffusion XL图像生成API使用说明

基本信息

  • API: https://openai/v1/chat/completions
  • 认证方式: Bearer Token
  • 请求格式: JSON
  • 支持的图像格式: PNG、JPG、WebP

认证说明

所有API请求都需要在请求头中包含认证信息:

Authorization: Bearer YOUR_API_KEY

核心功能: 文生图 (Text-to-Image)

1. 快速开始示例

以下是一个最简单的文生图请求示例:

curl -X POST "https://openai/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "prompt": {
        "3": {
            "inputs": {
                "seed": 123456789,
                "steps": 20,
                "cfg": 7,
                "sampler_name": "dpmpp_2m",
                "scheduler": "karras",
                "denoise": 1,
                "model": ["4", 0],
                "positive": ["6", 0],
                "negative": ["7", 0],
                "latent_image": ["5", 0]
            },
            "class_type": "KSampler"
        },
        "4": {
            "inputs": {
                "ckpt_name": "sd_xl_base_1.0.safetensors"
            },
            "class_type": "CheckpointLoaderSimple"
        },
        "5": {
            "inputs": {
                "width": 1024,
                "height": 1024,
                "batch_size": 1
            },
            "class_type": "EmptyLatentImage"
        },
        "6": {
            "inputs": {
                "text": "一只可爱的小猫咪",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "7": {
            "inputs": {
                "text": "模糊,低质量,变形,丑陋,水印,签名,文字",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "8": {
            "inputs": {
                "samples": ["3", 0],
                "vae": ["4", 2]
            },
            "class_type": "VAEDecode"
        },
        "9": {
            "inputs": {
                "filename_prefix": "my_generated_image",
                "images": ["8", 0]
            },
            "class_type": "SaveImage"
        }
    }
}'

2. 参数详细说明

核心参数配置

节点3 - KSampler (采样器配置)

"3": {
    "inputs": {
        "seed": 123456789,      // 随机种子, 控制生成结果的随机性
        "steps": 20,            // 采样步数, 推荐范围: 15-30
        "cfg": 7,               // 提示词引导强度, 推荐范围: 5-12
        "sampler_name": "dpmpp_2m", // 采样器类型
        "scheduler": "karras",  // 调度器类型
        "denoise": 1            // 去噪强度, 固定为1 (完全生成)
    },
    "class_type": "KSampler"
}

节点4 - 模型加载

"4": {
    "inputs": {
        "ckpt_name": "sd_xl_base_1.0.safetensors" // 使用的SDXL模型文件
    },
    "class_type": "CheckpointLoaderSimple"
}

节点5 - 图像尺寸设置

"5": {
    "inputs": {
        "width": 1024,          // 图像宽度(像素)
        "height": 1024,         // 图像高度(像素)
        "batch_size": 1         // 批量生成数量
    },
    "class_type": "EmptyLatentImage"
}

节点6 - 正面提示词

"6": {
    "inputs": {
        "text": "描述内容",  // 希望生成的图像描述
        "clip": ["4", 1]
    },
    "class_type": "CLIPTextEncode"
}

节点7 - 负面提示词

"7": {
    "inputs": {
        "text": "不希望出现的内容", // 不希望在图像中出现的内容
        "clip": ["4", 1]
    },
    "class_type": "CLIPTextEncode"
}

节点9 - 保存设置

"9": {
    "inputs": {
        "filename_prefix": "my_image", // 生成图片的文件名前缀
        "images": ["8", 0]
    },
    "class_type": "SaveImage"
}

3. 关键参数使用指南

Seed (随机种子)

  • 作用: 控制生成结果的随机性
  • 推荐值: 任意正整数, 如 123456789
  • 使用技巧:
    • 相同的 seed + 相同参数 = 相同结果 (可复现)
    • 不同的 seed = 不同的生成结果
    • 设置为 -1 可使用随机 seed

Steps (采样步数)

  • 作用: 控制生成质量和时间
  • 推荐范围: 15-30步
  • 使用建议:
    • 15-20步: 快速生成, 质量良好
    • 25-30步: 高质量生成, 耗时较长
    • 超过30步通常收益递减

CFG (提示词引导强度)

  • 作用: 控制AI对提示词的遵循程度
  • 推荐范围: 5-12
  • 使用建议:
    • 5-7: 自然, 创意性较强
    • 8-10: 平衡, 推荐使用
    • 11-12: 严格遵循提示词, 可能过度饱和

采样器选择

"sampler_name": "dpmpp_2m" // 推荐选项

常用采样器:

  • dpmpp_2m: 默认推荐, 质量好, 速度快
  • euler_a: 经典选择, 结果稳定
  • ddim: 确定性采样, 可复现性好

图像尺寸建议

标准尺寸:

  • 正方形: 1024×1024 (推荐)
  • 竖屏: 768×1024
  • 横屏: 1024×768
  • 宽屏: 1152×896

注意事项:

  • SDXL模型在1024×1024分辨率下效果最佳
  • 尺寸过大会增加显存消耗和生成时间
  • 建议保持宽高比接近原训练数据

4. 提示词编写指南

正面提示词 (Positive Prompt) 最佳实践

基本结构: [主体描述] + [环境/背景] + [风格/质量词汇] + [技术参数]

示例:

{
    "text": "一位优雅的女性, 穿着红色连衣裙, 站在樱花树下, 春天的公园, 柔和的阳光"
}

质量增强词汇:

  • 细节丰富, 精细, 锐利
  • 杰作, 最佳质量, 超现实主义

风格词汇:

  • 写实摄影, 油画风格, 水彩画, 素描
  • 赛博朋克, 蒸汽朋克, 奇幻风格
  • 极简主义, 复古风格, 现代艺术

负面提示词 (Negative Prompt) 最佳实践

通用负面词汇:

{
    "text": "模糊, 低质量, 变形, 丑陋, 扭曲, 多余的手指, 多余的手臂, 水印, 签名, 文字, logo, 边框, 裁剪不当"
}

质量相关:

  • 模糊, 低质量, 像素化, 噪点
  • 变形, 扭曲, 不协调, 比例失调

不想要的元素:

  • 水印, 签名, 文字, logo
  • 边框, 框架, 裁剪痕迹

5. 完整的使用流程

步骤1: 提交生成请求```bash

curl -X POST "https://openai/v1/chat/completions"
-H "Authorization: Bearer YOUR_API_KEY"
-H "Content-Type: application/json"
-d '{ "prompt": { # 完整参数配置 } }'
-w "HTTP Status: %{http_code}\n"``` 成功响应示例:

{
    "prompt_id": "12345678-abcd-efgh-ijkl-123456789012",
    "number": 1,
    "node_errors": {}
}

步骤2: 查询任务状态

curl -X GET "https://openai/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY"

响应示例:

{
    "exec_info": {
        "queue_remaining": 0
    },
    "queue_running": [
        {
            "prompt_id": "12345678-abcd-efgh-ijkl-123456789012",
            "number": 1
        }
    ],
    "queue_pending": []
}

步骤3: 获取生成结果

# 查看所有任务历史记录
curl -X GET "https://openai/v1/chat/completions/history" \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json"

# 查看特定任务的历史记录
curl -X GET "https://openai/v1/chat/completions/history/{prompt_id}" \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json"

成功响应示例:

{
    "{prompt_id}": {
        "prompt": {...},
        "outputs": {
            "9": {
                "images": [
                    {
                        "filename": "my_generated_image_00001_.png",
                        "subfolder": "",
                        "type": "output"
                    }
                ]
            }
        },
        "status": {
            "status_str": "success",
            "completed": true,
            "messages": []
        }
    }
}

步骤4: 下载生成的图片

curl -X GET "https://openai/v1/chat/completions/download/my_generated_image_00001_.png" \
-H "Authorization: Bearer YOUR_API_KEY" \
--output "downloaded_image.png"

6. 实用示例模板

人物肖像生成

{
    "prompt": {
        "3": {
            "inputs": {
                "seed": 987654321,
                "steps": 25,
                "cfg": 8,
                "sampler_name": "dpmpp_2m",
                "scheduler": "karras",
                "denoise": 1,
                "model": ["4", 0],
                "positive": ["6", 0],
                "negative": ["7", 0],
                "latent_image": ["5", 0]
            },
            "class_type": "KSampler"
        },
        "4": {
            "inputs": { "ckpt_name": "sd_xl_base_1.0.safetensors" },
            "class_type": "CheckpointLoaderSimple"
        },
        "5": {
            "inputs": { "width": 768, "height": 1024, "batch_size": 1 },
            "class_type": "EmptyLatentImage"
        },
        "6": {
            "inputs": {
                "text": "professional headshot of a business woman, confident smile, wearing elegant blazer, studio lighting, clean background, high resolution, professional photography",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "7": {
            "inputs": {
                "text": "blurry, low quality, deformed, ugly, distorted features, bad anatomy, watermark, signature",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "8": {
            "inputs": { "samples": ["3", 0], "vae": ["4", 2] },
            "class_type": "VAEDecode"
        },
        "9": {
            "inputs": { "filename_prefix": "portrait", "images": ["8", 0] },
            "class_type": "SaveImage"
        }
    }
}

风景场景生成

{
    "prompt": {
        "3": {
            "inputs": {
                "seed": 555666777,
                "steps": 20,
                "cfg": 7,
                "sampler_name": "dpmpp_2m",
                "scheduler": "karras",
                "denoise": 1,
                "model": ["4", 0],
                "positive": ["6", 0],
                "negative": ["7", 0],
                "latent_image": ["5", 0]
            },
            "class_type": "KSampler"
        },
        "4": {
            "inputs": { "ckpt_name": "sd_xl_base_1.0.safetensors" },
            "class_type": "CheckpointLoaderSimple"
        },
        "5": {
            "inputs": { "width": 1152, "height": 896, "batch_size": 1 },
            "class_type": "EmptyLatentImage"
        },
        "6": {
            "inputs": {
                "text": "serene mountain landscape at sunset, golden hour lighting, peaceful lake reflection, pine trees, dramatic clouds, cinematic composition, 8k resolution",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "7": {
            "inputs": {
                "text": "blurry, low quality, oversaturated, artificial, watermark, text, people, buildings",
                "clip": ["4", 1]
            },
            "class_type": "CLIPTextEncode"
        },
        "8": {
            "inputs": { "samples": ["3", 0], "vae": ["4", 2] },
            "class_type": "VAEDecode"
        },
        "9": {
            "inputs": { "filename_prefix": "landscape", "images": ["8", 0] },
            "class_type": "SaveImage"
        }
    }
}